import EXIF from 'exif-js';

export default {
	//上传图片函数 file文件；num上传给后台的图片类型：
	//imgSizeMax:图片任意一边的最大值，null或空，则读取配置$app.imgMaxSizeNum=4000
	//imgSizeMin:图片任意一边的最小值，null或空，则读取配置$app.imgMinSizeNum=600
	selectFileImage(imgSizeMax,imgSizeMin,fileIn, num, callBack,fail) {

		//如果有图片
		if (!!fileIn) {

			if(typeof fileIn === "string"){//单张base64 图片上传
				//大逻辑统一处理
				$app.allImgDo(num, {name:'图片裁切'}, fileIn, callBack,fail);
			}else{
				if ($app.judgeData(fileIn.size)) {//单文件 上传

					$app.sFileImg(fileIn,imgSizeMax,imgSizeMin, num, callBack,fail);
	
				} else {//多文件上传
	
					for (let i in fileIn) {
						let val = fileIn[i];
						if ($app.judgeData(val.size)) {
							$app.sFileImg(val,imgSizeMax,imgSizeMin, num, callBack,fail);
						}else{
							console.log('没有循环出文件，不给你上传')
						}
					}
	
				};
			}
		}else{
			console.log('没有选择文件，不给你上传')
		}
	},
	
	//图片上传 go-on
	//imgSizeMax:图片任意一边的最大值，null或空，则读取配置$app.imgMaxSizeNum=4000
	//imgSizeMin:图片任意一边的最小值，null或空，则读取配置$app.imgMinSizeNum=600
	sFileImg(file,imgSizeMax,imgSizeMin, num, callBack,fail) {

		(!!$app.showLoading) && ($app.loading('正在上传'));//用showLoading控制，显示 转转

		//iOS重力感应图片旋转90°处理
        EXIF.getData(file, ()=>{

			// let allEXIF = EXIF.getAllTags(file);
			// console.log('获取图片所有信息 = ' + JSON.stringify(allEXIF));
  
			//获取图片重力感应 Orientation参数：  1:0°  /  6:顺时针90°  /  8:逆时针180°  /  3:180°
			let orientation = EXIF.getTag(file, 'Orientation');
			// console.log('获取图片重力感应 = ' + orientation);


			//创建实例
			var reader = new FileReader();
			//异步读取文件内容
			reader.readAsDataURL(file);

			//读取操作成功完成
			reader.onload = e =>{
				//输出图片 图片/图片设置宽度 px/输出图片函数
				//获取图片重力感应 Orientation参数：  1:0°  /  6:顺时针90°  /  8:逆时针180°  /  3:180°
				$app.getImgData(e.target.result,orientation,imgSizeMax,imgSizeMin, data => {

					//这里可以使用校正后的图片data了
					// console.log('Base64图片 = ', data);

					if (num == 0) {
						callBack(data);
						fail instanceof Function && fail();
					} else {
						//大逻辑统一处理
						$app.allImgDo(num, file, data, callBack,fail);
					}
				});
			}
		});
		
	},
	
	//图片压缩
	getImgData(img,dir, imgSizeMax,imgSizeMin, callBack) {

		var setSizeMax=(!!imgSizeMax?(parseFloat(imgSizeMax)):$app.imgMaxSizeNum);
		var setSizeMin=(!!imgSizeMin?(parseFloat(imgSizeMin)):$app.imgMinSizeNum);

		var image = new Image(); //创建图片对象
		image.src = img; //初始化图片赋值

		//读取操作成功完成
		image.onload = ()=>{

			var degree = 0,
				drawWidth, drawHeight, width, height;
			drawWidth = image.naturalWidth;
			drawHeight = image.naturalHeight;

			

			// console.log('图片原本的比例尺寸 = ',drawWidth+'x'+drawHeight,';        setSizeMax:'+setSizeMax)
			// console.log('maxSide = ',maxSide)

			// 当图片的宽或高取最大值=》
			// 最大值 > setSizeMax($app.imgMaxSizeNum) 的时候，才会执行 =》压缩处理
			// 否则输出图片，为上传的原size
			
			// 先判断最大值 - 以下改变一下图片大小
			var maxSide = Math.max(drawWidth, drawHeight); //取最大值
			if (maxSide > setSizeMax) {
				var minSide = Math.min(drawWidth, drawHeight);
				minSide = minSide / maxSide * setSizeMax;
				maxSide = setSizeMax;
				if (drawWidth > drawHeight) {
					drawWidth = maxSide;
					drawHeight = minSide;
				} else {
					drawWidth = minSide;
					drawHeight = maxSide;
				}
			}

			// 再判断最小值 - 以下改变一下图片大小
			var minSide = Math.min(drawWidth, drawHeight); //取最小值
			if (minSide < setSizeMin) {
				var maxSide = Math.max(drawWidth, drawHeight);
				maxSide = maxSide / minSide  * setSizeMin;
				minSide = setSizeMin;
				if (drawWidth > drawHeight) {
					drawWidth = maxSide;
					drawHeight = minSide;
				} else {
					drawWidth = minSide;
					drawHeight = maxSide;
				}
			}
			


			var canvas = document.createElement('canvas');
			canvas.width = width = drawWidth;
			canvas.height = height = drawHeight;
			var context = canvas.getContext('2d');

			if (dir != undefined) {
				console.log('图片重力感应：' + dir);
				if($app.img_need_rotate()){
					//判断图片方向，重置canvas大小，确定旋转角度，iphone默认的是home键在右方的横屏拍摄方式
					switch (dir) {
						//iphone横屏拍摄，此时home键在左侧
						case 3:
						degree = 180;
						drawWidth = -width;
						drawHeight = -height;
						break;
						//iphone竖屏拍摄，此时home键在下方(正常拿手机的方向)
						case 6:
						canvas.width = height;
						canvas.height = width;
						degree = 90;
						drawWidth = width;
						drawHeight = -height;
						break;
						//iphone竖屏拍摄，此时home键在上方
						case 8:
						canvas.width = height;
						canvas.height = width;
						degree = 270;
						drawWidth = -width;
						drawHeight = height;
						break;
					}
				}
			} else {
				console.log('图片重力感应：无');
			}


			//使用canvas旋转校正
			context.rotate(degree * Math.PI / 180);
			context.drawImage(image, 0, 0, drawWidth, drawHeight);
			//返回校正图片   jpg图片格式/压缩质量0~1,
			callBack(canvas.toDataURL("image/jpeg", 0.85));

		}

	}


}; 